﻿using System;
using System.Linq;
using System.Reactive.Linq;
using System.Windows.Input;
using Present.Commands.Decorators;

namespace Present.Commands.Fluent
{
    public class ScheduledCommandBuilder<TCommand, TParameter> : CommandBuilder<ScheduledCommandDecorator<TCommand>, TParameter> where TCommand : ICommand
    {
        private readonly TCommand innerCommand;
        private TimeSpan timeSpan;

        public ScheduledCommandBuilder(TCommand innerCommand)
        {
            this.innerCommand = innerCommand;
        }

        public ScheduledCommandBuilder<TCommand, TParameter> Every(TimeSpan span)
        {
            timeSpan = span;
            return this;
        }

        public static implicit operator ScheduledCommandDecorator<TCommand>(ScheduledCommandBuilder<TCommand, TParameter> builder)
        {
            return builder.BuildCommand();
        }

        

        protected override ScheduledCommandDecorator<TCommand> BuildCommand()
        {
            return new ScheduledCommandDecorator<TCommand>(innerCommand) { Schedule = Observable.Interval(timeSpan) };
        }
    }
}